
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/

#include "paramLightDirectional.h"
#include "ILight.h"
#include "commonParameter.h"

#include <QLabel>
#include <QLineEdit>
#include <QGridLayout>
#include <QGroupBox>

#include "Scene.h"

#include "DirectionalLight.h"
#include "DirectionalAt.h"

//constructor
paramLightDirectional::paramLightDirectional(QWidget* parent) : baseParamWidget(parent)
{
	//Title
	_title = new QLabel("DIRECTIONAL LIGHT");

	//light position
	QGroupBox* groupPosition = new QGroupBox("Position", this);
	_positionXLabel = new QLabel("x=");
	_positionYLabel = new QLabel("y=");
	_positionZLabel = new QLabel("z=");
	_positionXLineEdit = new QLineEdit();
	_positionYLineEdit = new QLineEdit();
	_positionZLineEdit = new QLineEdit();
	QGridLayout* positionLayout = new QGridLayout();
	positionLayout->addWidget(_positionXLabel, 0, 0, 1, 1);
	positionLayout->addWidget(_positionXLineEdit, 0, 1, 1, 1);
	positionLayout->addWidget(_positionYLabel, 1, 0, 1, 1);
	positionLayout->addWidget(_positionYLineEdit, 1, 1, 1, 1);
	positionLayout->addWidget(_positionZLabel, 2, 0, 1, 1);
	positionLayout->addWidget(_positionZLineEdit, 2, 1, 1, 1);
	groupPosition->setLayout(positionLayout);

	//at position
	QGroupBox* groupAt = new QGroupBox("At", this);
	_atXLabel = new QLabel("x=");
	_atYLabel = new QLabel("y=");
	_atZLabel = new QLabel("z=");
	_atXLineEdit = new QLineEdit();
	_atYLineEdit = new QLineEdit();
	_atZLineEdit = new QLineEdit();
	QGridLayout* atLayout = new QGridLayout();
	atLayout->addWidget(_atXLabel, 0, 0, 1, 1);
	atLayout->addWidget(_atXLineEdit, 0, 1, 1, 1);
	atLayout->addWidget(_atYLabel, 1, 0, 1, 1);
	atLayout->addWidget(_atYLineEdit, 1, 1, 1, 1);
	atLayout->addWidget(_atZLabel, 2, 0, 1, 1);
	atLayout->addWidget(_atZLineEdit, 2, 1, 1, 1);
	groupAt->setLayout(atLayout);

	//direction
	QGroupBox* groupDirection = new QGroupBox("Direction", this);
	_dirXLabel = new QLabel("x=");
	_dirYLabel = new QLabel("y=");
	_dirZLabel = new QLabel("z=");
	_dirXLineEdit = new QLineEdit();
	_dirYLineEdit = new QLineEdit();
	_dirZLineEdit = new QLineEdit();
	QGridLayout* dirLayout = new QGridLayout();
	dirLayout->addWidget(_dirXLabel, 0, 0, 1, 1);
	dirLayout->addWidget(_dirXLineEdit, 0, 1, 1, 1);
	dirLayout->addWidget(_dirYLabel, 1, 0, 1, 1);
	dirLayout->addWidget(_dirYLineEdit, 1, 1, 1, 1);
	dirLayout->addWidget(_dirZLabel, 2, 0, 1, 1);
	dirLayout->addWidget(_dirZLineEdit, 2, 1, 1, 1);
	groupDirection->setLayout(dirLayout);

	//widget's layout
	_layout = new QGridLayout();

	_layout->addWidget(_title, 0, 0, 1, 2, Qt::AlignHCenter);
	_layout->addWidget(groupPosition, 1, 0, 1, 2, Qt::AlignHCenter);
	_layout->addWidget(groupAt, 2, 0, 1, 2, Qt::AlignHCenter);
	_layout->addWidget(groupDirection, 3, 0, 1, 2, Qt::AlignHCenter);
	
	_layout->addWidget(new QWidget(), 4, 0, 1, 2);
	_layout->setRowStretch(4, 1);

	setLayout(_layout);

	//connection to change the position of the light
	connect(_positionXLineEdit, SIGNAL(textEdited(const QString&)), this, SLOT(setObjectEyePosition()));
	connect(_positionYLineEdit, SIGNAL(textEdited(const QString&)), this, SLOT(setObjectEyePosition()));
	connect(_positionZLineEdit, SIGNAL(textEdited(const QString&)), this, SLOT(setObjectEyePosition()));

	//connection to change the position of the light
	connect(_atXLineEdit, SIGNAL(textEdited(const QString&)), this, SLOT(setObjectAtPosition()));
	connect(_atYLineEdit, SIGNAL(textEdited(const QString&)), this, SLOT(setObjectAtPosition()));
	connect(_atZLineEdit, SIGNAL(textEdited(const QString&)), this, SLOT(setObjectAtPosition()));

	//connection to change the position of the light
	connect(_dirXLineEdit, SIGNAL(textEdited(const QString&)), this, SLOT(setObjectDirectionPosition()));
	connect(_dirYLineEdit, SIGNAL(textEdited(const QString&)), this, SLOT(setObjectDirectionPosition()));
	connect(_dirZLineEdit, SIGNAL(textEdited(const QString&)), this, SLOT(setObjectDirectionPosition()));
}

//destructor
paramLightDirectional::~paramLightDirectional(){}

//fill all the widgets with values
void paramLightDirectional::fillWidgets(GUIILight* g)
{
	if (g->getType() == LT_DIRECTIONAL)
	{
		_directional = (DirectionalLight*)g;
		_at = _directional->getAtElement();
	}
	else
	{
		_at = (DirectionalAt*)g;
		_directional = _at->getDirectionalLightElement();
	}

	//set position
	_positionXLineEdit->setText(QString::number(_directional->getTranslate().x));
	_positionYLineEdit->setText(QString::number(_directional->getTranslate().y));
	_positionZLineEdit->setText(QString::number(_directional->getTranslate().z));

	//set at
	_atXLineEdit->setText(QString::number(_at->getTranslate().x));
	_atYLineEdit->setText(QString::number(_at->getTranslate().y));
	_atZLineEdit->setText(QString::number(_at->getTranslate().z));

	//set direction
	CVector3f dir = _at->getTranslate() - _directional->getTranslate();
	dir.normalize();
	_dirXLineEdit->setText(QString::number(dir.x));
	_dirYLineEdit->setText(QString::number(dir.y));
	_dirZLineEdit->setText(QString::number(dir.z));

}

//slot to set the position of the position widget
void paramLightDirectional::setPosition(float x, float y, float z)
{
	//set position
	_positionXLineEdit->setText(QString::number(_directional->getTranslate().x));
	_positionYLineEdit->setText(QString::number(_directional->getTranslate().y));
	_positionZLineEdit->setText(QString::number(_directional->getTranslate().z));

	//set at
	_atXLineEdit->setText(QString::number(_at->getTranslate().x));
	_atYLineEdit->setText(QString::number(_at->getTranslate().y));
	_atZLineEdit->setText(QString::number(_at->getTranslate().z));

	//set direction
	CVector3f dir = _at->getTranslate() - _directional->getTranslate();
	dir.normalize();
	_dirXLineEdit->setText(QString::number(dir.x));
	_dirYLineEdit->setText(QString::number(dir.y));
	_dirZLineEdit->setText(QString::number(dir.z));
}

//set the eye position
void paramLightDirectional::setObjectEyePosition()
{
	_directional->setTranslate(CVector3f(_positionXLineEdit->text().toFloat(),
		_positionYLineEdit->text().toFloat(),
		_positionZLineEdit->text().toFloat()));

	calculateNewDirection();

}

//set the at position
void paramLightDirectional::setObjectAtPosition()
{
	_at->setTranslate(CVector3f(_atXLineEdit->text().toFloat(),
		_atYLineEdit->text().toFloat(),
		_atZLineEdit->text().toFloat()));

	calculateNewDirection();

}

//set the up position
void paramLightDirectional::setObjectDirectionPosition()
{
	CVector3f dir(_dirXLineEdit->text().toFloat(),
		_dirYLineEdit->text().toFloat(),
		_dirZLineEdit->text().toFloat());
	dir.normalize();

	float length = (_at->getTranslate() - _directional->getTranslate()).norme();

	CVector3f newAt = _directional->getTranslate() + dir * length;
	_at->setTranslate(newAt);

	_dirXLineEdit->setText(QString::number(dir.x));
	_dirYLineEdit->setText(QString::number(dir.y));
	_dirZLineEdit->setText(QString::number(dir.z));
}

void paramLightDirectional::calculateNewDirection()
{
	CVector3f dir = _at->getTranslate() - _directional->getTranslate();
	dir.normalize();
	_dirXLineEdit->setText(QString::number(dir.x));
	_dirYLineEdit->setText(QString::number(dir.y));
	_dirZLineEdit->setText(QString::number(dir.z));
}